<template>
  <el-dialog
    :title="!dataForm.id ? '新建' : isDetail ? '详情' : '编辑'"
    :close-on-click-modal="false"
    append-to-body
    :visible.sync="visible"
    class="JNPF-dialog JNPF-dialog_center"
    lock-scroll
    width="800px"
  >
    <el-row :gutter="15" class="">
      <el-form
        ref="elForm"
        :model="dataForm"
        :rules="rules"
        size="small"
        label-width="120px"
        label-position="right"
      >
        <template v-if="!loading">
          <el-col :span="24">
            <el-form-item
              label="会议室名称"
              label-width="120px"
              prop="meetingroomname"
            >
              <el-input
                v-model="dataForm.meetingroomname"
                placeholder="请输入"
                clearable
                :style="{ width: '100%' }"
              >
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="会议室布局" prop="meetingroomlayout">
              <JNPF-UploadImg
                v-model="dataForm.meetingroomlayout"
                :fileSize="5"
                sizeUnit="MB"
                :limit="9"
              >
              </JNPF-UploadImg>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="会议室设备" prop="meetingroomequipment">
              <el-select
                v-model="dataForm.meetingroomequipment"
                placeholder="请选择"
                clearable
                :style="{ width: '100%' }"
                :multiple="true"
              >
                <el-option
                  v-for="(item, index) in meetingroomequipmentOptions"
                  :key="index"
                  :label="item.fullName"
                  :value="item.enCode"
                  :disabled="item.disabled"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="会议室简介" prop="meetingroomintro">
              <el-input
                v-model="dataForm.meetingroomintro"
                placeholder="请输入"
                clearable
                :style="{ width: '100%' }"
              >
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="会议室座周数量" prop="tableweek">
              <el-input
                v-model="dataForm.tableweek"
                placeholder="请输入"
                clearable
                :style="{ width: '100%' }"
              >
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="会议室四周数量" prop="allround">
              <el-input
                v-model="dataForm.allround"
                placeholder="请输入"
                clearable
                :style="{ width: '100%' }"
              >
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="会议室适合场景" prop="meetingscene">
              <el-select
                v-model="dataForm.meetingscene"
                placeholder="请选择"
                clearable
                :style="{ width: '100%' }"
              >
                <el-option
                  v-for="(item, index) in meetingsceneOptions"
                  :key="index"
                  :label="item.fullName"
                  :value="item.enCode"
                  :disabled="item.disabled"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </template>
      </el-form>
    </el-row>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false"> 取 消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" v-if="!isDetail">
        确 定</el-button
      >
    </span>
  </el-dialog>
</template>
<script>
import request from "@/utils/request";
import { getDataInterfaceRes } from "@/api/systemData/dataInterface";
import { getDictionaryDataSelector } from "@/api/systemData/dictionary";
export default {
  components: {},
  props: [],
  data() {
    return {
      visible: false,
      loading: false,
      isDetail: false,
      dataForm: {
        meetingroomname: "",
        meetingroomlayout: [],
        meetingroomequipment: [],
        meetingroomintro: "",
        tableweek: "",
        allround: "",
        meetingscene: "",
      },
      rules: {
        meetingroomname: [
          {
            required: true,
            message: "请输入",
            trigger: "blur",
          },
        ],
        meetingroomlayout: [
          {
            required: true,
            message: "请至少选择一个",
            trigger: "click",
          },
        ],
        meetingroomequipment: [
          {
            required: true,
            message: "请至少选择一个",
            trigger: "change",
          },
        ],
        meetingroomintro: [
          {
            required: true,
            message: "请输入",
            trigger: "blur",
          },
        ],
        tableweek: [
          {
            required: true,
            message: "请输入",
            trigger: "blur",
          },
        ],
        allround: [
          {
            required: true,
            message: "请输入",
            trigger: "blur",
          },
        ],
        meetingscene: [
          {
            required: true,
            message: "请选择",
            trigger: "change",
          },
        ],
      },
      meetingroomequipmentOptions: [],
      meetingsceneOptions: [],
    };
  },
  computed: {},
  watch: {},
  created() {
    this.getmeetingroomequipmentOptions();
    this.getmeetingsceneOptions();
  },
  mounted() {},
  methods: {
    getmeetingroomequipmentOptions() {
      getDictionaryDataSelector("316931776930906565").then((res) => {
        this.meetingroomequipmentOptions = res.data.list;
      });
    },
    getmeetingsceneOptions() {
      getDictionaryDataSelector("316931923312116165").then((res) => {
        this.meetingsceneOptions = res.data.list;
      });
    },
    clearData(data) {
      for (let key in data) {
        if (data[key] instanceof Array) {
          data[key] = [];
        } else if (data[key] instanceof Object) {
          this.clearData(data[key]);
        } else {
          data[key] = "";
        }
      }
    },
    init(id, isDetail) {
      this.dataForm.id = id || 0;
      this.visible = true;
      this.isDetail = isDetail || false;
      this.$nextTick(() => {
        this.$refs["elForm"].resetFields();
        if (this.dataForm.id) {
          this.loading = true;
          request({
            url: "/api/example/BusMeetingRoom/" + this.dataForm.id,
            method: "get",
          }).then((res) => {
            this.dataInfo(res.data);
            this.loading = false;
          });
        } else {
          this.clearData(this.dataForm);
        }
      });
      this.$store.commit("generator/UPDATE_RELATION_DATA", {});
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["elForm"].validate((valid) => {
        if (valid) {
          this.request();
        }
      });
    },
    request() {
      var _data = this.dataList();
      if (!this.dataForm.id) {
        request({
          url: "/api/example/BusMeetingRoom",
          method: "post",
          data: _data,
        }).then((res) => {
          this.$message({
            message: res.msg,
            type: "success",
            duration: 1000,
            onClose: () => {
              this.visible = false;
              this.$emit("refresh", true);
            },
          });
        });
      } else {
        request({
          url: "/api/example/BusMeetingRoom/" + this.dataForm.id,
          method: "PUT",
          data: _data,
        }).then((res) => {
          this.$message({
            message: res.msg,
            type: "success",
            duration: 1000,
            onClose: () => {
              this.visible = false;
              this.$emit("refresh", true);
            },
          });
        });
      }
    },
    dataList() {
      var _data = JSON.parse(JSON.stringify(this.dataForm));
      _data.meetingroomlayout = JSON.stringify(_data.meetingroomlayout);
      _data.meetingroomequipment = JSON.stringify(_data.meetingroomequipment);
      return _data;
    },
    dataInfo(dataAll) {
      let _dataAll = dataAll;
      _dataAll.meetingroomlayout = JSON.parse(_dataAll.meetingroomlayout);
      _dataAll.meetingroomequipment = JSON.parse(_dataAll.meetingroomequipment);
      this.dataForm = _dataAll;
    },
  },
};
</script>
